package com.kwedding.service.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.kwedding.bean.TB_Demo;
import com.kwedding.bean.TB_Style;
import com.kwedding.dao.TB_DemoMapper;
import com.kwedding.dao.TB_StyleMapper;
import com.kwedding.dto.web.RequestWebAddDemo;
import com.kwedding.dto.web.RequestWebDemoList;
import com.kwedding.dto.web.RequestWebUpDownDemo;
import com.kwedding.dto.web.RequestWebUpdateDemo;
import com.kwedding.dto.web.ResponseWebDemoList;
import com.kwedding.util.CheckedUtil;
import com.kwedding.util.Constants;
import com.kwedding.util.CopyPageInfo;
import com.kwedding.util.CopyerObject;
import com.kwedding.util.GsonUtil;
import com.kwedding.util.IDGenerate;
import com.kwedding.util.LoggerParent;
import com.kwedding.util.RequestErrorBackException;
import com.kwedding.util.Resp_PageInfo;
import springfox.documentation.spring.web.json.Json;

@Service
public class WebDemoService extends LoggerParent {
	@Autowired
	private TB_DemoMapper tb_DemoMapper;
	@Autowired
	private TB_StyleMapper tb_StyleMapper;

	/**
	 * 添加样片
	 * 
	 * @param requestWebInsertImages
	 */
	@Transactional
	public void insert(RequestWebAddDemo requestWebInsertImages) {
		CheckedUtil.isNull(requestWebInsertImages.getZn(), Constants.REQUEST_ERROR_1, "中文不能为空");
		CheckedUtil.isNull(requestWebInsertImages.getEn(), Constants.REQUEST_ERROR_2, "英文名称不能为空");
		CheckedUtil.isNull(requestWebInsertImages.getRefId(), Constants.REQUEST_ERROR_3, "分类不能为空");
		CheckedUtil.isNull(requestWebInsertImages.getImage(), Constants.REQUEST_ERROR_4, "图片不能为空");
		CheckedUtil.isNull(requestWebInsertImages.getBrandId(), Constants.REQUEST_ERROR_5, "品牌不能为空");
		TB_Demo tb_Demo = CopyerObject.copyerObject(requestWebInsertImages, TB_Demo.class);
		tb_Demo.setDemoId(IDGenerate.generate());
		tb_Demo.setImages(new Json(GsonUtil.listToJsonStr(requestWebInsertImages.getImages())));
		tb_Demo.setStatus(false);
		tb_Demo.setCreateTime(new Date());
		tb_Demo.setUpdateTime(new Date());
		if (tb_DemoMapper.insert(tb_Demo) != 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_6, "上传样片失败");
		}
	}

	/**
	 * 修改样片
	 * 
	 * @param requestWebUpdateDemo
	 */
	@Transactional
	public void update(RequestWebUpdateDemo requestWebUpdateDemo) {
		CheckedUtil.isNull(requestWebUpdateDemo.getDemoId(), Constants.REQUEST_ERROR_1, "请选择要修改的内容");
		CheckedUtil.isNull(requestWebUpdateDemo.getZn(), Constants.REQUEST_ERROR_1, "中文不能为空");
		CheckedUtil.isNull(requestWebUpdateDemo.getEn(), Constants.REQUEST_ERROR_2, "英文名称不能为空");
		CheckedUtil.isNull(requestWebUpdateDemo.getRefId(), Constants.REQUEST_ERROR_3, "分类不能为空");
		CheckedUtil.isNull(requestWebUpdateDemo.getImage(), Constants.REQUEST_ERROR_4, "图片不能为空");
		TB_Demo tb_Demo = tb_DemoMapper.selectByPrimaryKey(requestWebUpdateDemo.getDemoId());
		if (tb_Demo == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_5, "修改的数据不存在或已删除");
		}
		tb_Demo = CopyerObject.copyerObject(requestWebUpdateDemo, TB_Demo.class);
		if (requestWebUpdateDemo.getImages() != null && requestWebUpdateDemo.getImages().size() != 0) {
			tb_Demo.setImages(new Json(GsonUtil.listToJsonStr(requestWebUpdateDemo.getImages())));
		}
		if (tb_DemoMapper.updateBySelective(tb_Demo) != 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_6, "修改样片失败");
		}
	}

	/**
	 * web样片列表
	 * 
	 * @param requestWebSearchName
	 * @return
	 */
	@Transactional(readOnly = true)
	public Resp_PageInfo<ResponseWebDemoList> getDemoList(RequestWebDemoList requestWebDemoList) {
		CheckedUtil.isNull(requestWebDemoList.getBrandId(), Constants.REQUEST_ERROR_1, "品牌不能为空");
		int page = 1;
		if (requestWebDemoList.getPageNum() != null) {
			page = requestWebDemoList.getPageNum();
		}
		PageHelper.startPage(page, Constants.PAGESIZE);
		List<TB_Demo> tb_Demos = tb_DemoMapper.selectDemolist(requestWebDemoList.getSearchName(),
				requestWebDemoList.getBrandId(), requestWebDemoList.getStatus());
		List<ResponseWebDemoList> responseWebDemoLists = new ArrayList<>();
		if (tb_Demos != null && tb_Demos.size() > 0) {
			tb_Demos.stream().forEach(r -> {
				ResponseWebDemoList demoList = CopyerObject.copyerObject(r, ResponseWebDemoList.class);
				demoList.setIdStr(r.getDemoId().toString());
				// 设置风格名称
				TB_Style tb_Style = tb_StyleMapper.selectByPrimaryKey(r.getRefId());
				if (tb_Style != null) {
					demoList.setStyleName(tb_Style.getChineseName());
				}
				demoList.setBrandIdStr(r.getBrandId().toString());
				responseWebDemoLists.add(demoList);
			});
		}
		return CopyPageInfo.copyPageInfo(tb_Demos, responseWebDemoLists);
	}

	/**
	 * 上下架样片
	 * 
	 * @param upDownDemo
	 */
	@Transactional
	public void upDownDemo(RequestWebUpDownDemo upDownDemo) {
		CheckedUtil.isNull(upDownDemo.getDemoId(), Constants.REQUEST_ERROR_1, "请选择要修改的内容");
		TB_Demo tb_Demo = tb_DemoMapper.selectByPrimaryKey(upDownDemo.getDemoId());
		if (tb_Demo == null) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_2, "修改的数据不存在或已删除");
		}
		tb_Demo.setStatus(!tb_Demo.getStatus());
		tb_Demo.setUpdateTime(new Date());
		if (tb_DemoMapper.updateBySelective(tb_Demo) != 1) {
			throw new RequestErrorBackException(Constants.REQUEST_ERROR_3, "上下架样片失败");
		}
	}
}
